
const app = getApp();
function toDecimal(x) {
  var val = Number(x)
  if (!isNaN(parseFloat(val))) {
    val = val.toFixed(2);
  }
  return val;
}  
Page({
  data: {
     tagsList:'',
     pickerData:'',
     customItem: '全部',
     checkbox:false
  }, 
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    });
    var ageArr = [];
    for (var i = 0; i < 2; i++) {
      ageArr[i] = [];
      for (var ii = 0; ii <= 100; ii++) {
        if (ii == 0) {
          ageArr[i].push('无')
        } else {
          ageArr[i].push(ii)
        }
      };
    };
    that.setData({
      ageArr: ageArr
    });

    getCurrentPages()[getCurrentPages().length - 2].setData({
      tags: []
    })

    if (!getCurrentPages()[getCurrentPages().length - 2].data.tagsList){
      wx.request({
        url: app.globalData.phpUrl + 'Publish/api',
        method: "POST",
        header: { 'content-type': 'application/json' },
        data: { api_name: 'more_tags', openid: options.openOId },
        success: function (data) {
          var d = data.data.data;
          var l = d.length;
          var nd = [];
          var ndv = [];
          var select_type = [];
          for (var oo = 0; oo < l; oo++) {
            nd[oo] = [];
            ndv[oo] = [];
            select_type[oo] = [];

            if (d[oo].questions) {
              for (var pp = 0; pp < d[oo].questions.length; pp++) {
                nd[oo][pp] = [];
                ndv[oo][pp] = Number();
                select_type[oo][pp] = d[oo].questions[pp].type;
                if (d[oo].questions[pp].type == 1){
                  for (var uu = 0; uu < d[oo].questions[pp].option.length + 1; uu++) {
                    nd[oo][pp][uu] = [];
                    if (uu == 0) {
                      nd[oo][pp][uu] = '无'
                    } else if (uu > 0) {
                      nd[oo][pp][uu] = d[oo].questions[pp].option[uu - 1].content.toString();
                    }
                  }
                };
                if (d[oo].questions[pp].type == 2) {
                  nd[oo][pp] = {};
                  nd[oo][pp].price = 0;
                  nd[oo][pp].pre_price = 0;
                  nd[oo][pp].options = [];
                  for (var uu = 0; uu < d[oo].questions[pp].option.length; uu++) {
                    nd[oo][pp].options[uu]={};
                    nd[oo][pp].options[uu].content = d[oo].questions[pp].option[uu].content.toString();
                    nd[oo][pp].options[uu].id = uu + 1;
                    nd[oo][pp].options[uu].checked = '';
                  }
                }
                if (d[oo].questions[pp].type == 4) {
                  nd[oo][pp] = ['全部', '全部', '全部']
                }
              }
            }
          };

          that.setData({
            tagsList: d,
            pickerData: nd,
            pickerDataValue: ndv,
            select_type: select_type
          });

          wx.hideLoading()
        }
      });
    }else{
      that.setData({
        tagsList:getCurrentPages()[getCurrentPages().length - 2].data.tagsList,
        pickerData: getCurrentPages()[getCurrentPages().length - 2].data.pickerData,
        pickerDataValue: getCurrentPages()[getCurrentPages().length - 2].data.pickerDataValue,
        select_type: getCurrentPages()[getCurrentPages().length - 2].data.select_type
      });
      wx.hideLoading()
    }
  },
  
  signleChange:function(e){
    var that = this;
    var updata = that.data.pickerDataValue;
        updata[e.target.dataset.index][e.target.dataset.indexs] = e.detail.value; 
    that.setData({
      pickerDataValue: updata,
    });
  },

  checkboxChange: function (e) {

    var o;
    var oo;
    var that = this;
    var updata = that.data.pickerDataValue;
    var value = e.detail.value
    value = value.sort(function (x, y) {
      return x - y;
    })


    var pickerData = that.data.pickerData;

    var grade_fee = that.data.tagsList[e.target.dataset.index].grade_fee;
    var pre_grade_fee = Number(that.data.tagsList[e.target.dataset.index].pre_grade_fee);

    updata[e.target.dataset.index][e.target.dataset.indexs] = [];

    for (o = 0; o < pickerData[e.target.dataset.index][e.target.dataset.indexs].options.length; o++) {
      pickerData[e.target.dataset.index][e.target.dataset.indexs].options[o].checked = ''
    };

    that.setData({
      pickerDataValue: updata,
      pickerData: pickerData
    });

    for (oo = 0; oo < value.length; oo++) {
      pickerData[e.target.dataset.index][e.target.dataset.indexs].options[Number(value[oo]) - 1].checked = true;
      updata[e.target.dataset.index][e.target.dataset.indexs][oo] = pickerData[e.target.dataset.index][e.target.dataset.indexs].options[Number(value[oo]) - 1].content;
    };

    pickerData[e.target.dataset.index][e.target.dataset.indexs].price = Number(toDecimal(grade_fee * value.length));
    pickerData[e.target.dataset.index][e.target.dataset.indexs].pre_price = Number(toDecimal(pre_grade_fee * value.length));

    that.setData({
      pickerData: pickerData,
      pickerDataValue: updata
    });
  },

  areaChange: function (e) {
    var that = this;
    var updata = that.data.pickerDataValue;
    updata[e.target.dataset.index][e.target.dataset.indexs] = e.detail.value;
    var newg = that.data.tagsList[e.target.dataset.index];
    if (e.detail.value[1] == '全部'){
      newg.questions[e.target.dataset.indexs].arePri = that.data.tagsList[e.target.dataset.index].grade_fee
    } else if (e.detail.value[2] == '全部'){
      newg.questions[e.target.dataset.indexs].arePri = toDecimal(that.data.tagsList[e.target.dataset.index].grade_fee * 2)
    }else{
      newg.questions[e.target.dataset.indexs].arePri = toDecimal(that.data.tagsList[e.target.dataset.index].grade_fee * 3)
    };
    that.setData({
      ['tagsList[' + e.target.dataset.index + ']']: newg
    })

    that.setData({
      pickerDataValue: updata,
    });
  },
  
  bindAgeChange:function(e){
    var that = this;
    var value = e.detail.value;
    var index = e.target.dataset.index;
    var indexs = e.target.dataset.indexs
    that.setData({
      ['pickerDataValue[' + index + '][' + indexs + ']'] :value
    })
  },

  hideCheckbox:function(e){
      var that = this;
      var checkObj = {};
      checkObj.index = e.currentTarget.dataset.index;
      checkObj.indexs = e.currentTarget.dataset.indexs;
      checkObj.name = e.currentTarget.dataset.name
      var checkbox = that.data.checkbox;
      that.setData({
        checkbox: checkbox == false ? true :false,
        checkObj: checkObj
      })
  },

  confirm_moreTags:function(e){
     var that = this;
     var money = 0;
     var pre_money = 0;
     var tags2 = [];
     var select_type = that.data.select_type;
     for (var ii = 0; ii < that.data.pickerDataValue.length;ii++){
       for (var yy = 0; yy < that.data.pickerDataValue[ii].length;yy++){
         if (select_type[ii][yy] == 1){
           if (that.data.pickerDataValue[ii][yy] != '0') {
             money = toDecimal(Number(money) + Number(that.data.tagsList[ii].grade_fee));
             pre_money = toDecimal(Number(pre_money) + Number(that.data.tagsList[ii].pre_grade_fee));
             tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[0], 'value': that.data.tagsList[ii].questions[yy].option[that.data.pickerDataValue[ii][yy] - 1].tag_option, select_type: select_type[ii][yy] })
           }
         };


         if (select_type[ii][yy] == 2) {
           if (that.data.pickerDataValue[ii][yy] != '0') {
             money = toDecimal(Number(money) + Number(that.data.pickerData[ii][yy].price));
             pre_money = toDecimal(Number(pre_money) + Number(that.data.pickerData[ii][yy].pre_price));
             tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[0], 'value': that.data.pickerDataValue[ii][yy].toString(), select_type: select_type[ii][yy] })
           }
         };

         if (select_type[ii][yy] == 4) {
           if (that.data.pickerDataValue[ii][yy] != '0' && that.data.pickerDataValue[ii][yy][0] != '全部') {
             if (that.data.pickerDataValue[ii][yy][1] == '全部') {
               money = toDecimal(Number(money) + Number(that.data.tagsList[ii].grade_fee));
               pre_money = toDecimal(Number(pre_money) + Number(that.data.tagsList[ii].pre_grade_fee));
               tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[0], 'value': that.data.pickerDataValue[ii][yy][0], select_type: select_type[ii][yy] });
             } else if (that.data.pickerDataValue[ii][yy][2] == '全部') {
               money = toDecimal(Number(money) + Number(that.data.tagsList[ii].grade_fee)*2);
               pre_money = toDecimal(Number(pre_money) + Number(that.data.tagsList[ii].pre_grade_fee) * 2);
               tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[0], 'value': that.data.pickerDataValue[ii][yy][1], select_type: select_type[ii][yy] });
               tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[1], 'value': that.data.pickerDataValue[ii][yy][1], select_type: select_type[ii][yy] });
             } else {
               money = toDecimal(Number(money) + Number(that.data.tagsList[ii].grade_fee) * 3);
               pre_money = toDecimal(Number(pre_money) + Number(that.data.tagsList[ii].pre_grade_fee) * 3);
               tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[0], 'value': that.data.pickerDataValue[ii][yy][1], select_type: select_type[ii][yy] });
               tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[1], 'value': that.data.pickerDataValue[ii][yy][1], select_type: select_type[ii][yy] });
               tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[2], 'value': that.data.pickerDataValue[ii][yy][2], select_type: select_type[ii][yy] });
             }
           }
         }
         if (select_type[ii][yy] == 6) {
           if (that.data.pickerDataValue[ii][yy] != '0') {
             if (that.data.pickerDataValue[ii][yy][0] != 0 && that.data.pickerDataValue[ii][yy][1] != 0){
               money = toDecimal(Number(money) + Number(that.data.tagsList[ii].grade_fee));
               pre_money = toDecimal(Number(pre_money) + Number(that.data.tagsList[ii].pre_grade_fee));
               tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[0], 'value': that.data.pickerDataValue[ii][yy][0] + '~' + that.data.pickerDataValue[ii][yy][1], select_type: select_type[ii][yy] })
             }
           }
         }

       }
     };

     getCurrentPages()[getCurrentPages().length - 2].setData({
       tags2Money: Number(money),
       pre_tags2Money: Number(pre_money),
       allMoney2: toDecimal(Number(getCurrentPages()[getCurrentPages().length - 2].data.allMoney2) - Number(getCurrentPages()[getCurrentPages().length - 2].data.tags2Money)),
       pre_allMoney2: toDecimal(Number(getCurrentPages()[getCurrentPages().length - 2].data.pre_allMoney2) - Number(getCurrentPages()[getCurrentPages().length - 2].data.pre_tags2Money)),
     });

     
    getCurrentPages()[getCurrentPages().length - 2].setData({
      tags: tags2,
      allMoney2: toDecimal(Number(getCurrentPages()[getCurrentPages().length - 2].data.allMoney2) + Number(money)),
      pre_allMoney2: toDecimal(Number(getCurrentPages()[getCurrentPages().length - 2].data.pre_allMoney2) + Number(pre_money)),
      tagsList: that.data.tagsList,
      pickerData: that.data.pickerData,
      pickerDataValue: that.data.pickerDataValue,
      select_type: select_type
    });

    getCurrentPages()[getCurrentPages().length - 2].sum();
    getCurrentPages()[getCurrentPages().length - 2].ava();
    
    wx.navigateBack({
      delta: 1,
    })
  },
  onUnload: function (e) {
    var that = this;
    var money = 0;
    var pre_money = 0;
    var tags2 = [];
    var select_type = that.data.select_type;
    for (var ii = 0; ii < that.data.pickerDataValue.length; ii++) {
      for (var yy = 0; yy < that.data.pickerDataValue[ii].length; yy++) {
        if (select_type[ii][yy] == 1) {
          if (that.data.pickerDataValue[ii][yy] != '0') {
            money = toDecimal(Number(money) + Number(that.data.tagsList[ii].grade_fee));
            pre_money = toDecimal(Number(pre_money) + Number(that.data.tagsList[ii].pre_grade_fee));
            tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[0], 'value': that.data.tagsList[ii].questions[yy].option[that.data.pickerDataValue[ii][yy] - 1].tag_option, select_type: select_type[ii][yy] });
          }
        };

        if (select_type[ii][yy] == 2) {
          if (that.data.pickerDataValue[ii][yy] != '0') {
            money = toDecimal(Number(money) + Number(that.data.pickerData[ii][yy].price));
            pre_money = toDecimal(Number(pre_money) + Number(that.data.pickerData[ii][yy].pre_price));

            tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[0], 'value': that.data.pickerDataValue[ii][yy].toString(), select_type: select_type[ii][yy] })
          }
        };

        if (select_type[ii][yy] == 4) {
          if (that.data.pickerDataValue[ii][yy] != '0' && that.data.pickerDataValue[ii][yy][0] != '全部') {
            if (that.data.pickerDataValue[ii][yy][1] == '全部') {
              money = toDecimal(Number(money) + Number(that.data.tagsList[ii].grade_fee));
              pre_money = toDecimal(Number(pre_money) + Number(that.data.tagsList[ii].pre_grade_fee));
              tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[0], 'value': that.data.pickerDataValue[ii][yy][0], select_type: select_type[ii][yy] });
            } else if (that.data.pickerDataValue[ii][yy][2] == '全部') {
              money = toDecimal(Number(money) + Number(that.data.tagsList[ii].grade_fee) * 2);
              pre_money = toDecimal(Number(pre_money) + Number(that.data.tagsList[ii].pre_grade_fee) * 2);

              tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[0], 'value': that.data.pickerDataValue[ii][yy][1], select_type: select_type[ii][yy] });
              tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[1], 'value': that.data.pickerDataValue[ii][yy][1], select_type: select_type[ii][yy] });
            } else {
              money = toDecimal(Number(money) + Number(that.data.tagsList[ii].grade_fee) * 3);
              pre_money = toDecimal(Number(pre_money) + Number(that.data.tagsList[ii].pre_grade_fee) * 3);

              tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[0], 'value': that.data.pickerDataValue[ii][yy][1], select_type: select_type[ii][yy] });
              tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[1], 'value': that.data.pickerDataValue[ii][yy][1], select_type: select_type[ii][yy] });
              tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[2], 'value': that.data.pickerDataValue[ii][yy][2], select_type: select_type[ii][yy] });
            }
          }
        }
        if (select_type[ii][yy] == 6) {
          if (that.data.pickerDataValue[ii][yy] != '0') {
            if (that.data.pickerDataValue[ii][yy][0] != 0 && that.data.pickerDataValue[ii][yy][1] != 0) {
              money = toDecimal(Number(money) + Number(that.data.tagsList[ii].grade_fee));
              pre_money = toDecimal(Number(pre_money) + Number(that.data.tagsList[ii].pre_grade_fee));

              tags2.push({ 'type': that.data.tagsList[ii].grade_type, 'name': that.data.tagsList[ii].questions[yy].tag_name[0], 'value': that.data.pickerDataValue[ii][yy][0] + '~' + that.data.pickerDataValue[ii][yy][1], select_type: select_type[ii][yy] })
            }
          }
        }

      }
    };

    getCurrentPages()[getCurrentPages().length - 2].setData({
      tags2Money: Number(money),
      pre_tags2Money: Number(pre_money),
      allMoney2: toDecimal(Number(getCurrentPages()[getCurrentPages().length - 2].data.allMoney2) - Number(getCurrentPages()[getCurrentPages().length - 2].data.tags2Money)),
      pre_allMoney2: toDecimal(Number(getCurrentPages()[getCurrentPages().length - 2].data.pre_allMoney2) - Number(getCurrentPages()[getCurrentPages().length - 2].data.pre_tags2Money)),
    });


    getCurrentPages()[getCurrentPages().length - 2].setData({
      tags: tags2,
      allMoney2: toDecimal(Number(getCurrentPages()[getCurrentPages().length - 2].data.allMoney2) + Number(money)),
      pre_allMoney2: toDecimal(Number(getCurrentPages()[getCurrentPages().length - 2].data.pre_allMoney2) + Number(pre_money)),
      tagsList: that.data.tagsList,
      pickerData: that.data.pickerData,
      pickerDataValue: that.data.pickerDataValue,
      select_type: select_type
    });

    getCurrentPages()[getCurrentPages().length - 2].sum();
    getCurrentPages()[getCurrentPages().length - 2].ava();
  }
})